<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="../../../static/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../../static/js/jquery.js"></script>
<script type="text/javascript" src="../../../static/js/getBrandAndProduct.js"></script>
<script type="text/javascript">
function tipOpen(content,productId,productStatus) {
	$(".tipright p").text(content);
	$("#tip").fadeIn(200);
    let str = '<input name="" type="button"  class="sure" value="确定" onclick="tipClose('+productId+','+productStatus+')" />\n' +
            '      &nbsp;\n' +
            '      <input name="" type="button"  class="cancel" value="取消" onclick="tipClose('+productId+','+productStatus+')" />';
    $("#cpl_tip").html(str);
}
function tipClose(productId,productStatus) {
  if (productId!=null&&productStatus!=null) {
    var checkStatus = productStatus;
    if (productStatus === 2) {
      productStatus = 1;
    } else if (productStatus === 1) {
      productStatus = 2;
    }
    $.ajax({
      url: "/purchase/updateProduct",
      method: "post",
      data: {productId: productId, productStatus: productStatus},
      dataType: "JSON",
      success: function (rel) {
        window.location.reload();
      }
    })
  }
	$("#tip").fadeOut(200);
}
let pageSize = 5;
var cpConn = {
  brandId:null,
  typeName:null,
  productModel:null,
  productStatus:null
}

function getParamCPL() {
  var tyName = $("#cpl_TName").val();
  if (tyName === "请选择"){
    tyName = null;
  }
  var status = $("#cpl_Status").val();
  if (status === "请选择") {
    status = null;
  }
  var bName = $("#cpl_Bid").val();
  if (bName === "请选择") {
    bName = null;
  }
  cpConn = {
    brandId:bName,
    typeName:tyName,
    productModel:$("#cpl_Name").val(),
    productStatus:status
  };

}

function searchCPL(){
  getParamCPL();
  CPlQueryPage(1);
}

$(function () {
  brandListDDB();
  getParamCPL();
  CPlQueryPage(1)
})

function CPlQueryPage(currentPage) {
  cpConn.pageNum = currentPage;
  cpConn.pageSize = pageSize;
  $.ajax({
    url:"/purchase/queryProductPage",
    method:"post",
    data:cpConn,
    dataType:"JSON",
    success:function (rel) {
      let str = '';
      rel.data.data.forEach(function (product,i) {
        let checkStatusName;
        if (product.productStatus === "1") {
          checkStatusName = "可用";
        } else {
          checkStatusName = "不可用"
        }
        str += '<tr>\n' +
                '          <td>'+((++i)+(currentPage-1)*pageSize)+'</td>\n' +
                '          <td>'+product.brandName+'</td>\n' +
                '          <td>'+product.typeName+'</td>\n' +
                '          <td>'+product.productModel+'</td>\n' +
                '          <td>'+product.productUnit+'</td>\n' +
                '          <td>'+checkStatusName+'</td>\n' +
                '          <td>'+product.createTime+'</td>\n' +
                '          <td>'+product.createName+'</td>';
        if (checkStatusName === "可用"){
          str += '<td>\n' +
                  '            <a href="cpupdate?productId='+product.productId+'" class="tablelink">修改</a>\n' +
                  '            <a href="javascript:void(0)" class="tablelink" onclick="tipOpen(\'是否确认注销此条信息？\','+product.productId+','+product.productStatus+')">注销</a>\n' +
                  '          </td>\n' +
                  '        </tr>';
        }else {
          str += '<td>\n' +
                  '            <a href="cpupdate?productId='+product.productId+'" class="tablelink">修改</a>\n' +
                  '            <a href="javascript:void(0)" class="tablelink" onclick="tipOpen(\'是否确认恢复此条信息？\','+product.productId+','+product.productStatus+')">恢复</a>\n' +
                  '          </td>\n' +
                  '        </tr>';
        }
      })
      $("#cpl_tbody").html(str);
      //分页
      let array = '<div class="message">共<i class="blue">'+rel.data.totalCount+'</i>条记录，当前显示第&nbsp;<i class="blue">'+currentPage+'</i>页</div>';
      let beforeNum;
      if (currentPage>1){
        beforeNum = currentPage -1;
      }else {
        beforeNum = 1;
      }
      array +=' <ul class="paginList">\n' +
              '        <li class="paginItem"><a href="javascript:;" onclick="CPlQueryPage('+beforeNum+')"><span class="pagepre"></span></a></li>';
      //定义开始位置begin和结束位置end
      var begin;
      var end;
      if (rel.data.totalPage<5){
        //总页码不够5页
        begin = 1;
        end = rel.data.totalPage;
      }else {
        //总页码超过5页
        begin = rel.data.pageNum - 1;
        end = rel.data.pageNum + 3;
        if (begin<=1){
          begin = 1;
          end = begin + 4;
        }
        if (end >= rel.data.totalPage){
          end = rel.data.totalPage;
          begin = end - 4;
        }
      }
      for (let i = begin; i <= end; i++) {
        let li = '';
        if (i === rel.data.pageNum){
          li = '<li class="paginItem current" onclick="CPlQueryPage('+currentPage+')"><a href="javascript:;">'+currentPage+'</a></li>';
        }else {
          li = '<li class="paginItem" onclick="CPlQueryPage('+i+')"><a href="javascript:;">'+i+'</a></li>';
        }
        array += li;
      }
      let nextNum;
      if (currentPage>=rel.data.totalPage){
        nextNum = rel.data.totalPage;
      }else {
        nextNum = currentPage+1;
      }
      array += '<li class="paginItem"><a href="javascript:;" onclick="CPlQueryPage('+nextNum+')"><span class="pagenxt"></span></a></li>\n' +
              '      </ul>';
      $("#cpl_page").html(array);
    }
  })
}

</script>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>商品管理</li>
    <li>基本内容</li>
  </ul>
</div>
<div class="rightinfo">
  <form action="" method="post">
    <ul class="tools">
      <li> 品牌名称:
        <select id="cpl_Bid" class="dfBrand" onchange="productTypeListDDB()">
          <option value="">请选择</option>
<!--          <option>联想</option>-->
<!--          <option>海尔</option>-->
        </select>
      </li>
      <li> 商品类型:
        <select id="cpl_TName" class="dfProduct">
          <option value="">请选择</option>
<!--          <option>笔记本</option>-->
<!--          <option>电视机</option>-->
        </select>
      </li>
      <li> 商品型号:
        <input id="cpl_Name" type="text" />
      </li>
      <li> 状态：
        <select>
          <option id="cpl_Status">请选择</option>
          <option value="1">可用</option>
          <option value="2">不可用</option>
        </select>
      </li>
      <li class="subBut" onclick="searchCPL()"><img src="../../../static/images/t06.png" />查询</li>
      <li class="subBut" onclick="window.location.href='cpadd'"><img src="../../../static/images/t01.png" />添加</li>
    </ul>
    <table class="tablelist">
      <thead>
        <tr>
          <th>序号</th>
          <th>品牌名称</th>
          <th>商品类型</th>
          <th>商品型号</th>
          <th>单位</th>
          <th>状态</th>
          <th>创建时间</th>
          <th>创建人</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody id="cpl_tbody">
        <tr>
          <td>1</td>
          <td>联想</td>
          <td>笔记本</td>
          <td>X260</td>
          <td>台</td>
          <td>可用</td>
          <td>2013-09-09 15:05:05</td>
          <td>管理员</td>
          <td>
            <a href="cpupdate" class="tablelink">修改</a>
            <a href="javascript:void(0)" class="tablelink" onclick="tipOpen('是否确认注销此条信息？')">注销</a>
          </td>
        </tr>
        <tr>
          <td>2</td>
          <td>海尔</td>
          <td>电视机</td>
          <td>55寸4K液晶</td>
          <td>台</td>
          <td>不可用</td>
          <td>2013-09-09 15:05:05</td>
          <td>管理员</td>
          <td>
            <a href="cpupdate" class="tablelink">修改</a>
            <a href="javascript:void(0)" class="tablelink" onclick="tipOpen('是否确认恢复此条信息？')">恢复</a>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="pagin" id="cpl_page">
      <div class="message">共<i class="blue">1256</i>条记录，当前显示第&nbsp;<i class="blue">2&nbsp;</i>页</div>
      <ul class="paginList">
        <li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>
        <li class="paginItem"><a href="javascript:;">1</a></li>
        <li class="paginItem current"><a href="javascript:;">2</a></li>
        <li class="paginItem"><a href="javascript:;">3</a></li>
        <li class="paginItem"><a href="javascript:;">4</a></li>
        <li class="paginItem"><a href="javascript:;">5</a></li>
        <li class="paginItem more"><a href="javascript:;">...</a></li>
        <li class="paginItem"><a href="javascript:;">10</a></li>
        <li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>
      </ul>
    </div>
  </form>
  <!-- 提示框 -->
  <div id="tip" class="tip">
    <div class="tiptop"><span>提示信息</span><a onclick="tipClose()"></a></div>
    <div class="tipinfo"> <span><img src="../../../static/images/ticon.png" /></span>
      <div class="tipright">
        <p></p>
        <cite>如果是请点击确定按钮 ，否则请点取消。</cite> </div>
    </div>
    <div class="tipbtn" id="cpl_tip">
      <input name="" type="button"  class="sure" value="确定" onclick="tipClose()" />
      &nbsp;
      <input name="" type="button"  class="cancel" value="取消" onclick="tipClose()" />
    </div>
  </div>
</div>
<script type="text/javascript">
	$('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
